package com.lf.controller;

import com.lf.model.EbProduct;
import com.lf.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        //1.获取新闻表的第1页数据，每页3条
        List<EbProduct> products = productService.list(page,pageSize);
        int maxPage = productService.getMaxPage(pageSize);
        //2.将数据(list)和视图(jsp)返回给spring mvc中央控制
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("products",products);//key-value
        mv.addObject("curPage",page);
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("manage/product");
        return mv;
    }
    @RequestMapping("/to_save")
    public String toSave() {
        return "manage/product-add";
    }

    @RequestMapping("/save")
    public String save(EbProduct ebProduct, MultipartFile pictrueFile, HttpServletRequest request){
        System.out.println("进入ProductController.save==="+ebProduct+"\tpic=="+pictrueFile);
        //1.将数据添加到数据库(调用service)
        //1.1. 参数校验

        //1.2 将商品的信息保存到数据
        //1.2.1 设置商品的图片的名字
        String originFileName = pictrueFile.getOriginalFilename();
        ebProduct.setEpFileName(originFileName);

        int result = productService.save(ebProduct);
        //1.3 文件上传（图片从客户端的电脑上上传到服务器(tomcat  images/products) ）
        //1.3.1指定上传到服务器的哪个路劲
        String rootPath = request.getServletContext().getRealPath("/");
        System.out.println("root:"+rootPath);
        String filePath = rootPath+"WEB-INF/images/product/"+originFileName;
        //创建文件
        File file =new File(filePath);
        if(!file.exists()){
            file.mkdir();
        }
        //1.3.2 上传
        try {
            pictrueFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("上传文件成功！！");
        //2.将成功的视图发送给 spring mvc的中央控制器

        return "manage/manage-result";
    }

    @RequestMapping("/edit")
    public String edit(Model model, Integer epId){
        EbProduct ebProduct = productService.selectByCondition (epId);
        model.addAttribute("ebProduct",ebProduct);
        return "manage/product-modify";
    }
@RequestMapping("/editSubmit")
    public  String editSubmit(EbProduct ebProduct){
        int result = productService.updateById(ebProduct);
        if (result<=0){
            return "manage/error";
        }
        return "manage/manage-result";
}





@RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        int result = productService.delete(id);
        if (result==0){

        }
        return "manage/manage-result";
}

}